package top.drinyeit.weilong.retrofit;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionPool;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class OkHttpManager {

    private static OkHttpManager m_ins = null;

    public static OkHttpManager getClient() {
        if (m_ins == null) {
            synchronized (OkHttpManager.class) {
                if (m_ins == null) {
                    m_ins = new OkHttpManager();
                }
            }
        }
        return m_ins;
    }

    public OkHttpClient getOkHttp() {
//        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.retryOnConnectionFailure(false);
        builder.connectionPool(new ConnectionPool());
        builder.connectTimeout(10, TimeUnit.SECONDS);
        builder.readTimeout(10, TimeUnit.SECONDS);
        builder.writeTimeout(10, TimeUnit.SECONDS);
//        builder.addInterceptor(logging);

        builder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request original = chain.request();
                HttpUrl.Builder builder1 = original.url()
                        .newBuilder();
//                        .setEncodedQueryParameter("token", "")
//                        .setEncodedQueryParameter("user_id", "");

                Request request = original.newBuilder()
                        .method(original.method(), original.body())
                        .url(builder1.build())
                        .build();

                return chain.proceed(request);
            }
        });
        return builder.build();
    }
}
